﻿Imports System.Data.OleDb

Public Class addTorontoPicture
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub btnAddNew_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAddNew.Click
        Dim myGUID As New System.Guid
        myGUID = Guid.NewGuid()

        Dim newPictureName = myGUID.ToString() + ".jpg"

        If fu_Picture.HasFile Then
            Dim location = Request.MapPath("pictures") & "/" & newPictureName
            fu_Picture.SaveAs(location)
        End If

        Dim LatLag As String = ""

        If ddLocation.SelectedItem.Text = "Toronto Downtown" Then
            LatLag = "43.670906,-79.393331"
        ElseIf ddLocation.SelectedItem.Text = "Toronto Island" Then
            LatLag = "43.616815,-79.382899"
        ElseIf ddLocation.SelectedItem.Text = "Etobicoke" Then
            LatLag = "43.664162,-79.545282"
        ElseIf ddLocation.SelectedItem.Text = "Scarborough" Then
            LatLag = "43.769175,-79.246262"
        ElseIf ddLocation.SelectedItem.Text = "East York" Then
            LatLag = "43.698811,-79.334438"
        ElseIf ddLocation.SelectedItem.Text = "North York" Then
            LatLag = "43.751665,-79.426225"
        End If


        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("dbDusanCS").ConnectionString)
        Dim SqlString As String = "Insert into TorontoPictures(PictureOwner,TorontoPictureName,Description,Details, Location, LatLag, YearTaken,Picture,Active) Values (@param1, @param2,@param3,@param4,@param5,@param6,@param7,@param8,True)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@param1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@param2", tb_PictureName.Text)
        cmd.Parameters.AddWithValue("@param3", tb_Description.Text)
        cmd.Parameters.AddWithValue("@param4", tb_Details.Text)
        cmd.Parameters.AddWithValue("@param5", ddLocation.SelectedItem.Text)
        cmd.Parameters.AddWithValue("@param6", LatLag)
        cmd.Parameters.AddWithValue("@param7", tb_YearTaken.Text)
        cmd.Parameters.AddWithValue("@param8", "pictures/" & newPictureName)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()


        'Find inserted TorontoPicture ID

        Dim torontoPictureID As Int32

        SqlString = "SELECT TOP 1 ID FROM TorontoPictures ORDER BY 1 DESC"
        cmd = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text

        Dim readerID As OleDbDataReader = cmd.ExecuteReader()

        If readerID.HasRows Then
            Do While readerID.Read()
                torontoPictureID = readerID.GetInt32(0)
            Loop
        End If

        'Insert inside TorontoPicturterTagsJunction Tag Toronto

        SqlString = "Insert into TorontoPicturesTagsJunction (FK_TorontoPictures,FK_Tags) Values (@param1, 1)"
        cmd = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@param1", torontoPictureID)
        cmd.ExecuteNonQuery()

        oleDbConn.Close()

        Response.Redirect("ThankYou.aspx?back=TorontoPicturesList.aspx&message=added new picture")

    End Sub

End Class